﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YouxinHelper.Helper.Currency;

namespace YouxinHelper.Youxin.Fans
{
    class FansOperation
    {

        public static void Start(string Uid, String Token,string FllowId) {
            if (Uid == "" || Token == "")
                return;
            Console.WriteLine("开始解析");
           

            AnalysisUserInfo(Uid,Token, FllowId);
        }

  
        private static void AnalysisUserInfo(string Uid, String Token, string FllowId,string orderKey = "")
        {
            string Shtml = WebHelper.HttpGet(string.Format(FansStaticValue.FansAPI, orderKey, FllowId), FansStaticValue.CurrencyWebHeader(Uid, Token));
           
            FansStaticValue.Root ReadJson = JsonConvert.DeserializeObject<FansStaticValue.Root>(Shtml);
            foreach (FansStaticValue.ListItem SingleInfo in ReadJson.msg.list) {
                Console.WriteLine(SingleInfo.uid);
           
                if (SingleInfo.relation == 102||SingleInfo.relation==0) {
                    Console.WriteLine("未关注,启动关注"+ "Code" + SingleInfo.relation);
                 
                    Thread.Sleep(1000);
                    StartAddFans(SingleInfo.uid, FansStaticValue.CurrencyWebHeader(Uid, Token));
                }
            }
            if (ReadJson.msg.orderKey != "") {
                Console.WriteLine("启动下次扫描："+ ReadJson.msg.orderKey);

                AnalysisUserInfo(Uid, Token, FllowId,ReadJson.msg.orderKey);
            }
        }

        private static void StartAddFans(int uid, WebHeaderCollection webHeaderCollection)
        {
            string API = "https://youxin.357.com/v2/user/addfriend";
            webHeaderCollection.Add("Content-Type", "application/x-www-form-urlencoded");
            webHeaderCollection.Add("User-Agent", "youxin/4.0.3.6(Android;samsung 4.4.2)YouXin");
            String PostData = "fid=" + uid;
            Console.WriteLine(WebHelper.PostDataForAjax(API, PostData, webHeaderCollection)); ;
        }
    }
}
